package table_comm

import (
	"time"

	"gitee.com/klein-x/biz/model/mysql/table"
	"gitee.com/klein-x/biz/module/db/mysql"
	"gorm.io/gorm"
)

type CommGoods struct {
	GoodsID     string         `gorm:"type:char(36);primaryKey;unique;not null;comment:'商品ID'"`
	Name        string         `gorm:"type:varchar(255);not null;comment:'商品名称'"`
	Summary     string         `gorm:"type:text;comment:'摘要信息'"`
	Thumbnail   string         `gorm:"type:char(36);default:'';comment:'缩略图UUID'"`
	PageInfo    string         `gorm:"type:text;comment:'页面信息'"`
	BuyerCount  uint           `gorm:"default:0;comment:'购买人数'"`
	Price       uint64         `gorm:"type:bigint unsigned;default:0;comment:'价格(分)'"`
	ChapterID   string         `gorm:"type:char(36);comment:'题库UUID'"`
	Duration    uint64         `gorm:"type:bigint unsigned;default:0;comment:'时效(秒)'"`
	Status      string         `gorm:"type:char(1);default:'0';comment:'商品状态, normal 0'"`
	PublishedAt *time.Time     `gorm:"comment:'发布时间'"`
	CreatedAt   time.Time      `gorm:"comment:'创建时间'"`
	UpdatedAt   time.Time      `gorm:"comment:'更新时间'"`
	DeletedAt   gorm.DeletedAt `gorm:"comment:'软删除时间'"`
}

func (bg *CommGoods) TableName() string {
	return "bank_goods"
}

func GoodsGetByKeys(keys []string) (*[]CommGoods, error) {
	db := mysql.GetGlobalDB()

	var goodsInfoList []CommGoods

	return table.SelectCheck(&goodsInfoList,
		db.Where("goods_id IN (?)", keys).
			Find(&goodsInfoList),
	)
}

func GoodsGetByKey(key string) (*CommGoods, error) {
	db := mysql.GetGlobalDB()
	var goods CommGoods
	return table.SelectCheck(&goods, db.Where("goods_id=?", key).First(&goods))
}
